package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.config.OSSFileUtil;
import com.bwie.mapper.TbDetailMapper;
import com.bwie.mapper.TbItemMapper;
import com.bwie.mapper.TbRegionMapper;
import com.bwie.pojo.TbDetail;
import com.bwie.pojo.TbItem;
import com.bwie.pojo.TbRegion;
import com.bwie.service.TbItemService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TbItemServiceImpl extends ServiceImpl<TbItemMapper, TbItem> implements TbItemService {
    @Autowired
    TbItemMapper tbItemMapper;
    @Autowired
    OSSFileUtil ossFileUtil;
    @Autowired
    TbRegionMapper tbRegionMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    TbDetailMapper tbDetailMapper;
    @Override
    public R pageTbItemList(PageInfoVo pageInfoVo) {

        Page<TbItem> tbItemPage=new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());

        QueryWrapper<TbItem> queryWrapper=new QueryWrapper<>();

        if (!StringUtils.isEmpty(pageInfoVo.getKeyWord())) {
            queryWrapper.lambda().like(TbItem::getItemName,pageInfoVo.getKeyWord());
        }
        if (!StringUtils.isEmpty(pageInfoVo.getKeyName())) {
            queryWrapper.lambda().like(TbItem::getProprietorName,pageInfoVo.getKeyName());
        }
        if (pageInfoVo.getStatus()!=null && pageInfoVo.getStatus()!=0) {
            queryWrapper.lambda().eq(TbItem::getItemStatus,pageInfoVo.getStatus());
        }
        if (pageInfoVo.getAddr()!=null && pageInfoVo.getAddr() != 0) {
            queryWrapper.lambda().eq(TbItem::getRegionId,pageInfoVo.getAddr());
        }
        if (!StringUtils.isEmpty(pageInfoVo.getBeginTime()) && !StringUtils.isEmpty(pageInfoVo.getEndTime())) {
            queryWrapper.lambda().le(TbItem::getCreateTime,pageInfoVo.getBeginTime())
                    .ge(TbItem::getCreateTime,pageInfoVo.getEndTime());
        }

        Page<TbItem> itemPage = tbItemMapper.selectPage(tbItemPage, queryWrapper);

        return R.OK(itemPage);

    }

    @Override
    public R saveTbItem(TbItem tbItem) {

        if(tbItem.getRight()==1) {
            tbItem.setItemStatus(1);
        }else {
            tbItem.setItemStatus(0);
        }
        TbRegion tbRegion = tbRegionMapper.selectById(tbItem.getRegionId());
        tbItem.setRegionName(tbRegion.getRegionName());
        tbItem.setCreateTime(new Date());

        tbItemMapper.insert(tbItem);

        for (TbDetail tbDetail : tbItem.getTbDetails()) {
            tbDetail.setItemId(tbItem.getItemId());
            tbDetail.setItemName(tbItem.getItemName());
            tbDetail.setCreateTime(new Date());
            tbDetailMapper.insert(tbDetail);
        }
        return R.OK();
    }

    @Override
    public R OssUpload(MultipartFile file) throws IOException {

        String s = ossFileUtil.uploadFileByBreakingPoint(file);
        return R.OK(s);
    }

    @Override
    public R TbRegionList() {

        List<TbRegion> tbRegions = tbRegionMapper.selectList(null);
        return R.OK(tbRegions);
    }
}
